Code:
#include <stdio.h> // Header File For Standard Input / Output
#include <stdarg.h> // Header File For Variable Argument Routines
#include <string.h> // Header File For String Management
#include <stdlib.h>
#include <math.h>
#include <string>
#include <iostream>
#include <windows.h>
//#include <gl/glut.h>
//#include <GL/freeglut.h>
//#include <GL/glui.h>
//#include <GL/openglut.h>
//#include <GL/openglut_exp.h>
//#include <GL/openglut_ext.h>
//#include <GL/openglut_std.h>
//#include <GL/glut.h>
//#include <GL/glutf90.h>
//#include <GL/glaux.h>
#include <GL/freeglut.h>
#include <GL/freeglut_ext.h>
#include <GL/freeglut_std.h>
using namespace std;
#define FREEGLUT_STATIC
//#define GLUT_STATIC
// Constants -----------------------------------------------------------------
// Height and width of window. You may wish to change this depending upon
// your program's needs.
#define kWindowWidth 800
#define kWindowHeight 600
const double PI = acos(-1.0);
// At a depth of -5.0 along the z axis, -2 to 2 ranges over virtually the entire
// window.
const double screenDepth = -5.0;
const double screenLeftLimit = -2.0;
const double screenRightLimit = 2.0;
const double screenUpLimit = 2.0;
const double screenDownLimit = -2.0;
const double screenRangeX = 100.0;
const double screenRangeY = 100.0;
// Let's make a structure!
// Structures are fun!
struct color{
double rVal;
double gVal;
double bVal;
}WHITE, BLACK, RED, GREEN, BLUE, MAGENTA, TEAL, YELLOW;
void generateColors(){
WHITE.rVal = 1.0; WHITE.gVal = 1.0; WHITE.bVal = 1.0;
BLACK.rVal = 0.0; BLACK.gVal = 0.0; BLACK.bVal = 0.0;
}
void setColor(color col){
glColor3f(col.rVal, col.gVal, col.bVal);
}
void drawNestedQuadz(double x, double y, double xSize, double ySize,
double ratio, color outer, color inner){
double xShift = ratio*xSize;
double yShift = ratio*ySize;
// Draw 'outer' square
setColor(outer);
glBegin(GL_QUADS);
glVertex2f(x, y);
glVertex2f(x+xSize, y);
glVertex2f(x+xSize, y+ySize);
glVertex2f(x, y+ySize);
glEnd();
setColor(inner);
glBegin(GL_QUADS);
glVertex2f(x+xShift, y+yShift);
glVertex2f(x+xSize-xShift, y+yShift);
glVertex2f(x+xSize-xShift, y+ySize-yShift);
glVertex2f(x+xShift, y+ySize-yShift);
glEnd();
}
// TETRIS GLOBAL VARIABLES
bool board[10][20];
double xSizeBoard = 2.0;
double ySizeBoard = 4.0;
double xPositionBoard = -1.0;
double yPositionBoard = -2.0;
double boardRatio = .9;
void displayBoard(){
double xSizeBlock = xSizeBoard/10.0;
double ySizeBlock = ySizeBoard/20.0;
for(int i=0;i<10;i++){
for(int j=0;j<20;j++){
if(board[i][j]){
drawNestedQuadz(xPositionBoard+i*xSizeBlock, yPositionBoard + j*ySizeBlock,
xSizeBlock, ySizeBlock, boardRatio, WHITE, BLACK);
}
}
}
}
double randomPercentage(){
return (double)(rand()%100)/100.0;
}
// Piece Structure
struct piece{
bool layout[4][4];
color outerColor;
color innerColor;
};
piece pieces[28];
void generatePieces(){
for(int i=0; i< 28; i++){
for(int j=0;j<4;j++){
for(int k=0;k<4;k++){
pieces[i].layout[j][k] = 0;
}
}
}
pieces[0].layout[2][3] = 1;
pieces[0].layout[3][2] = 1;
pieces[0].layout[3][3] = 1;
pieces[0].layout[4][3] = 1;
pieces[0].outerColor = BLACK;
pieces[0].innerColor = WHITE;
}
void addPiece(int reference, int x, int y){
for(int i=0; i<4;i++){
for(int j=0; j<4;j++){
if(pieces[reference].layout[i][j]){
board[x+i][y+j] =1;
}
}
}
}
// Function Prototypes -------------------------------------------------------
// Should be included (and later defined) in all programs
GLvoid InitGL(GLvoid);
GLvoid DrawGLScene(GLvoid);
GLvoid ReSizeGLScene(int Width, int Height);
GLvoid Idle(GLvoid);
GLvoid Keyboard(unsigned char key, int x, int y);
GLvoid drawText();
// Main ---------------------------------------------------------------------
// Drives the good graphics
int main(int argc, char** argv)
{
generatePieces();
addPiece(0, 2, 2);
addPiece(0, 7, 7);
addPiece(0, 7, 11);
// Set up the graphics window
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(kWindowWidth, kWindowHeight);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
generateColors();
// Create the graphics object
InitGL();
// Associate customized functions with the graphics object
glutDisplayFunc(DrawGLScene);
glutReshapeFunc(ReSizeGLScene);
glutIdleFunc(Idle);
glutKeyboardFunc(Keyboard);
// Start running the graphics object
glutMainLoop();
return 0;
}
// InitGL -------------------------------------------------------------------
// You really don't need to worry about this; just make certain to include it
// in every program.
GLvoid InitGL(GLvoid)
{
glEnable(GL_TEXTURE_2D); // Enable Texture Mapping
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // This Will Clear The Background Color To Black
glClearDepth(1.0); // Enables Clearing Of The Depth Buffer
glDepthFunc(GL_LEQUAL); // The Type Of Depth Test To Do
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glShadeModel(GL_SMOOTH); // Enables Smooth Color Shading
glBlendFunc(GL_SRC_ALPHA, GL_ONE); // Select The Type Of Blending
glMatrixMode(GL_PROJECTION);
glLoadIdentity(); // Reset The Projection Matrix
gluPerspective(45.0f, (GLfloat) kWindowWidth / (GLfloat) kWindowHeight, 0.1f, 100.0f);
glMatrixMode(GL_MODELVIEW);
}
// Idle -------------------------------------------------------------------
// What the program does if the user does not press any keys
GLvoid Idle(GLvoid)
{
glutPostRedisplay();
}
// Keyboard --------------------------------------------------------------
// This determines what happens when a button on the keyboard is pressed
// or the mouse clicked. Registers 'key' pressed, or x and y positions
// of the mouse click.
//
// You may, at times, wish to modify it.
GLvoid Keyboard(GLubyte key, int x, int y)
{
glutPostRedisplay(); // Current does nothing except redisplay the window
}
// ReSizeGLScene ------------------------------------------------------------
// Don't touch this--this is what happens if the window is resized
// (if you grab the lower-right corner and drag)
//
GLvoid ReSizeGLScene(int Width, int Height)
{
glViewport (0, 0, (GLsizei) Width, (GLsizei) Height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (GLfloat) Width / (GLfloat) Height, 0.1, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
// DrawGLScene -----------------------------------------------------
// What is drawn every time that the window displays.
// This is the only part of the program that you will be editing,
// for now.
GLvoid DrawGLScene(GLvoid)
{
// Don't change this part
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
glEnable(GL_TEXTURE_2D); // Enable texturing
glLoadIdentity(); // Reset The View
glTranslatef(0.0f,0.0f,-5.0f); // Move Into The Screen 5 Units
/* INSERT YOUR PROGRAM HERE */
// Draw your scene--this is the part that you will edit
displayBoard();
/* STOP INSERTING YOUR PROGRAM HERE */
// Also critical. Do not delete.
glutSwapBuffers();
glFlush();
}
"""""""""""""""""""""""""""""""""""""""""""""""""" """"""""""""""""""""""